package com.example.demo.generation.web;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.generation.entity.Columns;
import com.example.demo.generation.entity.Config;
import com.example.demo.generation.service.ColumnsService;
import com.example.demo.generation.util.FileUtils;
import com.example.demo.generation.util.StringUtils;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
@RequestMapping("/generation")
public class ColumnsWeb {


    
    @Autowired
    ColumnsService columnsService;
    
    @RequestMapping("/getColumns")
    public Object getusers(){
    	
    	//读取配置文件
        String configString=FileUtils.readString("doc/config.txt");
        Config config=FileUtils.getConfig(configString);
        
        //读取表的列信息
    	List<Columns> columns=columnsService.selectByTable(config.getTableName(), "test");
    	for(Columns column: columns) {
    		//System.out.println(column.getColumnName()+"--"+column.getColumnComment()+"--"+column.getColumnType()+"--"+column.getColumnKey());
    	}
        
        //获取字符串
        String entityString=FileUtils.readString("doc/template/User.java");
        String mapperString=FileUtils.readString("doc/template/UserMapper.java");
        String serviceString=FileUtils.readString("doc/template/UserService.java");
        String serviceImplString=FileUtils.readString("doc/template/UserServiceImpl.java");
        String utilString=FileUtils.readString("doc/template/UserUtil.java");
        String webString=FileUtils.readString("doc/template/UserWeb.java");
        
        //替换模版
        entityString=StringUtils.myReplace(entityString,config);
        mapperString=StringUtils.myReplace(mapperString,config);
        serviceString=StringUtils.myReplace(serviceString,config);
        serviceImplString=StringUtils.myReplace(serviceImplString,config);
        utilString=StringUtils.myReplace(utilString,config);
        webString=StringUtils.myReplace(webString,config);
        

    	//util 工具生成
    	String tableFilter=StringUtils.getStringFilter(columns);
    	//字段生成
    	String tableFiled=StringUtils.getTableFiled(columns);
    	//get set工具
    	String tableGetAndSet=StringUtils.getTableGetAndSet(columns);
    	String tableID=StringUtils.getTableId(columns);
    	
        //字符过滤
        utilString=StringUtils.myReplaceTableFilter(utilString,tableFilter);
        //替换
        entityString=StringUtils.myReplaceEntityString(entityString,tableFiled,tableGetAndSet,tableID);
        
        //路径
        String entityDir="doc/out/"+config.getDirName()+"/entity";
        String entityFileName=config.getUpperJavaTableName()+".java";
        
        String mapperDir="doc/out/"+config.getDirName()+"/mapper";
        String mapperFileName=config.getUpperJavaTableName()+"Mapper.java";
        
        String serviceDir="doc/out/"+config.getDirName()+"/service";
        String serviceFileName=config.getUpperJavaTableName()+"Service.java";
        
        String serviceImplDir="doc/out/"+config.getDirName()+"/service/impl";
        String serviceImplFileName=config.getUpperJavaTableName()+"ServiceImpl.java";

        String utilImplDir="doc/out/"+config.getDirName()+"/util";
        String utilFileName=config.getUpperJavaTableName()+"Util.java";

        String webDir="doc/out/"+config.getDirName()+"/web";
        String webFileName=config.getUpperJavaTableName()+"Web.java";
        
        FileUtils.writeString(entityString,entityDir,entityFileName);
        FileUtils.writeString(mapperString,mapperDir,mapperFileName);
        FileUtils.writeString(serviceString,serviceDir,serviceFileName);
        FileUtils.writeString(serviceImplString,serviceImplDir,serviceImplFileName);
        FileUtils.writeString(utilString,utilImplDir,utilFileName);
        FileUtils.writeString(webString,webDir,webFileName);
        System.out.println("成功！！！！！！！！！");
        return columns;
    }

}
